package org.ithang.tools.timer;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.ithang.application.bcAddr.bean.BcAddr;
import org.ithang.application.bcAddr.service.BcAddrService;
import org.ithang.application.btRecord.service.BtRecordService;
import org.ithang.application.operationLog.bean.OperationLog;
import org.ithang.application.operationLog.service.OperationLogService;
import org.ithang.system.keyvalue.bean.Keyvalue;
import org.ithang.system.keyvalue.service.KeyvalueService;
import org.ithang.system.sysLog.bean.SysLog;
import org.ithang.system.sysLog.service.SysLogService;
import org.ithang.system.user.bean.User;
import org.ithang.tools.eth.ETHInfo;
import org.ithang.tools.eth.ETHUtil;
import org.ithang.tools.lang.DateUtils;
import org.ithang.tools.lang.JsonUtils;
import org.ithang.tools.lang.MacUtil;
import org.ithang.tools.lang.StrUtils;
import org.ithang.tools.model.ErrorInfo;
import org.ithang.tools.redis.Keys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;


/**
 * 定时归集
 * @author zyt
 *
 */
@JobHandler(value="collectHandler")
@Component
public class CollectHandler extends IJobHandler {
	
	@Autowired
	private KeyvalueService keyvalueService;

	@Autowired
	private SysLogService sysLogService;
	
	@Autowired
	private BcAddrService bcAddrService;
	
	@Autowired
	private BtRecordService btRecordService;
	
	@Autowired
	private OperationLogService operationLogService;
	
	
	@Override
	public ReturnT<String> execute(String param) throws Exception {
		XxlJobLogger.log("开始归集");
		try{
			Thread goverThread=new Thread(new Runnable(){
				@Override
				public void run() {
					
					try{
					//归集
					List<BcAddr> addrs=bcAddrService.list();
					
					

				boolean test=false;
					    Keyvalue kv=keyvalueService.get(Keys.TestDev);
					    if(null!=kv&&1==kv.intValue()){
					    	test=true;
					    }
					
					for(BcAddr addr:addrs){
						try{
							if(ETHUtil.checkAccount(addr.getAddr())){
							    double balance=ETHUtil.getTokenBalance(addr.getAddr(),test);
							    if(balance>0){
							    	 ETHInfo info=ETHUtil.transfer(StrUtils.dhx(addr.getPrivate_key()), keyvalueService.getInAddr(), balance,test);
									    if(null!=info){
									    	SysLog log=new SysLog();
									    	log.setSlog(JsonUtils.toJsonStr(info));
									    	log.setCtime(DateUtils.getSystime());
									    	sysLogService.add(log);
									    }
							    }
							}
						}catch(Exception e){
							//不做处理,继续归集
							e.printStackTrace();
							SysLog log=new SysLog();
					    	log.setSlog(e.getMessage());
					    	log.setCtime(DateUtils.getSystime());
					    	sysLogService.add(log);
						}
					}
				
					OperationLog log=new OperationLog();
					log.setContent("归集用户USDT");
					log.setCreate_time(DateUtils.getSystime());
					log.setIp("system");
					log.setUser_id(0);
					
					operationLogService.add(log);
					}catch(Exception e){
						e.printStackTrace();
					}
				}
			});
			goverThread.start();
		}catch(Exception e){
			e.printStackTrace();
			return FAIL;
		}
		XxlJobLogger.log("归集结束");
		
		return SUCCESS;
	}

}
